using System;
using System.Collections.Generic;
using System.Text;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Exceptions;

namespace SchEdu.Framework.Validators
{
    public class FlowShopOperationValidator : Validator
    {
        public FlowShopOperationValidator(SchEduData schEduData)
            : base(schEduData)
        {
        }

        #region IValidator Members

        public override void Validate()
        {
            if(SchEduData.Order.Count == 0)
            {
                return;
            }
            SchEduDataSet.OrderRow firstOrder = SchEduData.Order[0];
            OperationSortedList firstSequence = new OperationSortedList(firstOrder.GetOperationRows());

            foreach (SchEduDataSet.OrderRow order in SchEduData.Order)
            {
                OperationSortedList sequence = new OperationSortedList(SchEduData.Order[0].GetOperationRows());
                if (firstSequence.Count != sequence.Count)
                {
                    throw new ValidatorException(string.Format(Resources.SameNumberOfOperations, firstOrder.OrderNo, order.OrderNo));
                }

                if(firstSequence.Count == 0 || sequence.Count == 0)
                {
                    continue;
                }

                for (int i=0; i < firstSequence.Count; i++)
                {
                    if (firstSequence.Values[i].WorkCenterID != sequence.Values[i].WorkCenterID)
                    {
                        throw new ValidatorException(string.Format(Resources.SameOperationWorkCenter, 
                            firstOrder.OrderNo, firstSequence.Values[i].OperationNo,
                            order.OrderNo, sequence.Values[i].OperationNo));
                    }
                }
            }
        }

        #endregion
    }
}
